
/*Create county population 1960-2019*/
/*insert directory here*/
use ctypop60-00, clear
drop if year>1990
drop if ctyfip==86 & statefip==12
append using ctypop91-20
replace ctyfip=86 if ctyfip==25 & statefip==12 & year>1990

merge 1:1 year ctyfip statefip using geoid2cty
drop if year==2020
drop if statefip==2 | statefip==15
replace pop=2331 if ctyfip==113 & statefip==46 & _merge==2
replace pop=6302 if ctyfip==515 & statefip==51 & _merge==2
replace pop=4215 if ctyfip==515 & statefip==51 & _merge==2
replace _merge=3 if _merge==2 & statefip==46
replace _merge=3 if _merge==2 & statefip==51
keep if _merge==3
drop _merge geoid
save ctypop60-19, replace

/*Create personal income rate of change*/
use persinc59-19, clear
drop if statefip==2 | statefip==15
sort statefip ctyfip year
gen chpersinc=(persinc-persinc[_n-1])/persinc[_n-1]
replace chpersinc=0 if chpersinc==.
keep if year>1959 & year<2020
sort statefip ctyfip year
gen aggchpersinc=.
replace aggchpersinc=1 if year==2011
forvalues y=1/51{
replace aggchpersinc=aggchpersinc[_n+1]/(1+chpersinc) if year==2011-`y'
}
forvalues y=2012/2019{
replace aggchpersinc=aggchpersinc[_n-1]/(1-chpersinc) if year==`y'
}
egen statepersinc=max(persinc), by(year statefip)
gen stateshare=persinc/statepersinc
drop if ctyfip==0

/*Create emissions damage 1960-2019*/
merge m:1 statefip ctyfip using emissions2011
drop _merge
gen noxdam=nox2011*aggchpersinc
gen pm25dam=pm252011*aggchpersinc
gen so2dam=so22011*aggchpersinc
save damfac, replace
replace noxdam=nox2011*aggchpersinc*stateshare
replace pm25dam=pm252011*aggchpersinc*stateshare
replace so2dam=so22011*aggchpersinc*stateshare
collapse (sum) so2dam pm25dam noxdam, by(statefip year)
gen ctyfip=0
append using damfac
keep statefip ctyfip year so2dam pm25dam noxdam
merge m:1 year using SCC
keep if _merge==3
drop _merge scm2019
rename scc2019 co2dam
merge m:1 year using CPI34-2019
keep if _merge==3
replace so2dam=so2dam*255.7/CPI
replace co2dam=co2dam*255.7/CPI
replace noxdam=noxdam*255.7/CPI
replace pm25dam=pm25dam*255.7/CPI
label var so2dam "2019 $/ton"
label var co2dam "2019 $/ton"
label var noxdam "2019 $/ton"
label var pm25dam "2019 $/ton"
drop CPI _merge
save damfac, replace

use damfac, clear
merge m:1 year using emissionscontentbyfuel
keep if _merge==3
local varlist ho ng propane
local varlist2 co2 so2 pm25 nox
foreach i of local varlist{
foreach j of local varlist2{
gen `i'`j'dam=`j'dam*`i'`j'
}
gen `i'dam=`i'co2dam+`i'so2dam+`i'noxdam+`i'pm25dam
}
keep statefip year ctyfip propanedam ngdam hodam
merge 1:1 year statefip ctyfip using elecdam
drop if _merge==2
drop _merge
save envdamage, replace


